package com.biostime.springdraw.modules.sd.web;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.biostime.springdraw.common.config.Global;
import com.biostime.springdraw.common.persistence.Page;
import com.biostime.springdraw.common.utils.StringUtils;
import com.biostime.springdraw.common.web.BaseController;
import com.biostime.springdraw.modules.sd.entity.Award;
import com.biostime.springdraw.modules.sd.entity.Awardtype;
import com.biostime.springdraw.modules.sd.service.AwardService;
import com.biostime.springdraw.modules.sd.service.AwardtypeService;
import com.biostime.springdraw.modules.sys.entity.User;

/**
 * 奖品表Controller
 * @author king
 * @version 2016-01-18
 */
@Controller
@RequestMapping(value = "${adminPath}/sd/award")
public class AwardController extends BaseController {

	@Autowired
	private AwardService AwardService;
	
	@Autowired
	private AwardtypeService awardtypeService;
	
	@ModelAttribute
	public Award get(@RequestParam(required=false) String id) {
		Award entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = AwardService.get(id);
		}
		if (entity == null){
			entity = new Award();
		}
		return entity;
	}

	@RequiresPermissions("sd:award:view")
	@RequestMapping(value = {"list", ""})
	public String list(Award Award, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Award> page = AwardService.findPage(new Page<Award>(request, response), Award); 
		model.addAttribute("page", page);
		return "modules/sd/AwardList";
	}

	@RequiresPermissions("sd:award:view")
	@RequestMapping(value = "form")
	public String form(Award Award, Model model) {
		model.addAttribute("Award", Award);
		if(Award != null && Award.getName() != null){
			String str =  Award.getName().replaceAll("\"", "&#34");
			Award.setName(str); 
		}
		List<Awardtype> awardtypeList = awardtypeService.findList(new Awardtype()); 
		model.addAttribute("awardtypeList", awardtypeList);				
		return "modules/sd/AwardForm";
	}

	@RequiresPermissions("sd:award:edit")
	@RequestMapping(value = "save")
	public String save(Award Award, Model model, RedirectAttributes redirectAttributes) {
		Award.setCreateBy(new User("0"));
		Award.setCreateDate(new Date());
		Award.setUpdateBy(new User("0"));
		Award.setUpdateDate(new Date());
		if(Award.getCouponId() != null && !Award.getCouponId().equals("")){
			Award.setIsCoupon("1");
		}else{
			Award.setIsCoupon("0");
		}		
		if (!beanValidator(model, Award)){
			return form(Award, model);
		}
		AwardService.save(Award);
		addMessage(redirectAttributes, "保存成功");
		return "redirect:"+Global.getAdminPath()+"/sd/award/?repage";
	}
	
	@RequiresPermissions("sd:award:delete")
	@RequestMapping(value = "delete")
	public String delete(@RequestParam(value="ids",required=false) String idsRequest, Award Award, RedirectAttributes redirectAttributes) {
		if(!StringUtils.isEmpty(idsRequest)){
			String[] ids = idsRequest.split(",");
			for(String id : ids){
				AwardService.delete(new Award(id));
			}
		}else{
			AwardService.delete(Award);
		}		
		addMessage(redirectAttributes, "删除成功");
		return "redirect:"+Global.getAdminPath()+"/sd/award/?repage";
	}

}